/*
 * @Descripttion:
 * @version: V1.0
 * @Author: ZYP
 * @Date: 2025-05-15 09:30:47
 * @LastEditors: ZYP
 * @LastEditTime: 2025-11-04 11:08:21
 */
#include <stdio.h>
#include "stdbool.h"

#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
// #include "tusb_cdc_acm.h"    todo why not find

#ifndef __ZYP_USB_H
#define __ZYP_USB_H

#define IS_ENABLE_RECIVE_RANDOEM_DATA 1
#define RANDOEM_DATA_STARTBIT       0xBA
#define RANDOEM_DATA_TANSFER_FLAG   0X00
#define RANDOEM_DATA_FLAG           0XAA
#define RANDOEM_DATA_END_BIT        0xAB


#define IS_SUPOORT_BUST_TEST 1
#define BUS_TEST_DATASIZE 13

#define IS_ENABLE_DEBUG 0
#define USB_FIEL_SEND 1
#define N0_USB_FIEL_SEND 0

#define ADD_WATCH_DOG 1
#define NO_WATCH_DOG 0
#define MAX_FILENAME_LEN 32

#define TINY_USB_WRITE_SIZE 1024
#define IS_ENABLEN_SECOND_RESOVTION 0
#define FILE_OFFET 102400 /// 100KB

#define PSRAM_START_ADDR 0x3c040b4c                                    /// heap_caps_malloc addr
#define SAVA_LLF_FILE_STARTADDR PSRAM_START_ADDR                       /// 0x3c030a88    0x3c040b4c
#define SAVA_DNLD_FILE_STARTADDR SAVA_LLF_FILE_STARTADDR + FILE_OFFET  /// 0x3C049A88            3C059B4C
#define SAVA_MAIN_FILE_STARTADDR SAVA_DNLD_FILE_STARTADDR + FILE_OFFET /// 0x3C062A88             3C072B4C

// #define LLF_FILE_START_BIT     0xaabbcc00
// #define DNLD_FILE_START_BIT     0xaabbcc01
// #define MAIN_FILE_START_BIT     0xaabbcc02

#define LLF_FILE_START_BIT 0x00ccbbaa
#define DNLD_FILE_START_BIT 0x01ccbbaa
#define MAIN_FILE_START_BIT 0x02ccbbaa
#define DEBUG_FILE_START_BIT 0X09ccbbaa

#define FFU_FILE_START_BIT 0X03ccbbaa


#define IS_FILE_DEBUG 1
#define IS_FFU_MDDE  1


#define LLF_FILE 0
#define DNLD_FILE 1
#define MAIN_FILE 2 ///>100KB
#define MAIN_FILE_Done 3

#define SCIRIPT_FILE 5
#define FFU_FILE 6 

#define OTHER_FILE 7

#define TINY_USB_RECIVE_MAX_SIZE 64

#define VIGINAL_MODE_HARD_STARTBIT_NO_FILE_SIZE 1

#if VIGINAL_MODE_HARD_STARTBIT_NO_FILE_SIZE
// #define LLF_FILE_SIZE 30208 ///      LLF_FILE_SIZE_9070_1ce 30208 ,but other llf 30720     here is can or not  hrad code？   hard code   当前假设V1.2版本，后面FW编译的文件大小都是比这个大的                  here 判断只是最后几次才判断   if ((current_file_index == LLF_FILE && totall_bytes_received > LLF_FILE_SIZE - TINY_USB_RECIVE_MAX_SIZE) || (current_file_index == DNLD_FILE && totall_bytes_received > DNLD_FILE_SIZE - TINY_USB_RECIVE_MAX_SIZE) || (current_file_index == MAIN_FILE && totall_bytes_received > MAIN_FILE_SIZE - TINY_USB_RECIVE_MAX_SIZE))
// #define DNLD_FILE_SIZE 12288

#define LLF_FILE_SIZE 29696
#define DNLD_FILE_SIZE 11776
#define MAIN_FILE_SIZE 89600

// #define DUG_FLIE_SIZE      40*1024
#define DUG_FLIE_SIZE 40448 + 8 - 64

#define MAIN_FILE_SIZE_9050 79872

#endif

#define LLF_FILE_SIZE_KB 31 /// here is not can or no code？
#define DNLD_FILE_SIZE_KB 13
#define MAIN_FILE_SIZE_KB 88

#define LLF_FILE_START_FLAG 0X00000000
#define DNLD_FILE_START_FLAG 0X0000001
#define MAIN_FILE_START_FLAG 0X0000002

#define LLF_FILE_START_4BIT 0X89B2EC3B /// 3BECB289
#define DNLD_FILE_START_4BIT 0X89B2EC3B
#define MAIN_FILE_START_4BIT 0XB2EC3B02 /// 023BECB2
#define DUGBIN_FILE_START_FLAG 0X89B2EC3B

#define FILE_SLOT_SIZE (100 * 1024) // EVERY FILE ALLOC 100KB
#define MAX_FILES 3
#define FLIE_START_BIT 4
#define FLIE_END_BIT 4
#define  FIEL_END_MARKER 0xAAAAAAAA

#define IS_ENABLE_USB_CMD_HANLDE 1
#define IS_ENABLE_FACTORY_MODE_PROCESS 1
#define IS_EN_SEMAPHORE 1 /// WHY ENALE VIRIGIL CMD25 ERR?

extern uint8_t *psram_base;
extern uint32_t current_file_index;
extern size_t totall_bytes_received;
extern size_t totall_bytes_received_data;
extern bool receiving_file;
extern bool usb_data_send_flag;
extern uint32_t expected_end_marker;
extern size_t bytes_read;
extern QueueHandle_t usb_cmd_queue;
extern QueueHandle_t usb_data_queue;
#if IS_ENABLE_FACTORY_MODE_PROCESS
extern QueueHandle_t usb_factory_mode_queue;
#endif

extern size_t data_cmd_cnt;
extern uint8_t *cmd_data;
extern bool host_connected;

#if IS_EN_SEMAPHORE
extern SemaphoreHandle_t xInitSemaphore;
#endif

#if IS_FILE_DEBUG

#define MIN_SCIRT_FILE_SIZE DUG_FLIE_SIZE - TINY_USB_RECIVE_MAX_SIZE
extern uint32_t debug_file_index_startbit;
extern bool debugbinfile_reciveover;

#endif

#if IS_FFU_MDDE
extern bool ffu_binfile_reciveover;
extern uint32_t ffu_file_index_startbit;
#endif

#if VIGINAL_MODE_HARD_STARTBIT_NO_FILE_SIZE
#pragma pack(1)
typedef struct Virigin_File_Size
{
    unsigned int dnld_file_size;
    unsigned int llf_file_size;
    unsigned int main_file_size;
#if IS_FILE_DEBUG
    unsigned int debug_file_size;
#endif

#if IS_FFU_MDDE
    unsigned int ffu_file_size;
#endif
} Virigin_File_Size_T;
#pragma pack()
#endif

// uint32_t front_4Byte;   ///
typedef enum
{
    LLF_FILE_STATE,
    DNLD_FILE_STATE,
    MAIN_FILE_STATE,
    DEBUG_FILE_SATE,
} file_transfer_state_t;

typedef enum
{
    VIRGIN_FILE_MODE = 100,
    DEBUG_CMD_MODE,
    DATA_CMD_MODE,
    FILE_SCRIPT_MODE,
    BUST_TEST_MODE,
    PROGRAM_CID_CSD_MODE,
    Recivde_RANDOME_DATA_MOED,            ///less 64-5= 69 byte
#if IS_FFU_MDDE    
    FFU_FIEL_MODE,
#endif    
    OTHER_MODE,
} USB_CDC_MODE;
#if VIGINAL_MODE_HARD_STARTBIT_NO_FILE_SIZE
extern Virigin_File_Size_T virigin_file_size_t;
#endif
void zyp_send_file_via_usb(int fileIndex);
void print_file_hex(int fileIndex);
void init_tud_usb_usart_psram(void);
uint32_t convert_to_big_endian(uint32_t value);
void zyp_tinyusb_cdcacm_write_queue(uint8_t itf, uint8_t *write_buffer, size_t in_size);
bool zyp_tud_cdc_connected(void);
void zyp_finall_tinyusb_cdcacm_write_queue(uint8_t itf, uint8_t *write_buffer, size_t write_size);

void USB_CMD_TASK(void *pvParameters);

#endif
